/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.archive.portlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.archive.portlet.model.Archive;
import cn.com.chinasoa.archive.runtime.ArchiveRuntime;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * 删除文档
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-8-3下午04:23:26
 */
public class ArchiveDeleteActionFilter extends BaseRequestFilter implements
		ActionFilter {

	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {
		Repository tRepository = ArchiveRuntime.getRepository(actionRequest);
		if(tRepository != null) {
			try {
				String nodeName = actionRequest.getParameter(ArchiveUploadHelper.ARCHIVE_NODE_NAME);
				if(nodeName!=null){
			        Session session=null;
					try {
						session = tRepository.login(new SimpleCredentials(ArchiveUploadHelper.SEP,ArchiveUploadHelper.X4EE.toCharArray()));
			            Node root = session.getRootNode();
			            Node archiveNode = root.getNode(nodeName);
			            if(archiveNode!=null){
			            	archiveNode.remove();
			            	session.save();
							Map archiveModelMap = new HashMap();
					        for(NodeIterator nIt = root.getNodes();nIt.hasNext();){
					        	Node tArchiveNode = (Node)nIt.next();
					            String tArchiveNodeName = tArchiveNode.getName();
					            if((tArchiveNodeName!=null)&&(tArchiveNodeName.startsWith(ArchiveUploadHelper.ARCHIVE_PREX))){
					            	Property tArchiveNamePty = tArchiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_NAME);
							        Property tArchiveTitlePty = tArchiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_TITLE);
							        Property tArchiveUserPty = tArchiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_USER);
							        Property tArchiveCalenderPty = tArchiveNode.getProperty(ArchiveUploadHelper.ARCHIVE_CALENDAR);
							        
							        String tArchiveName = tArchiveNamePty.getValue().getString();
							        String tArchiveTitle = tArchiveTitlePty.getValue().getString();
							        String tArchiveUser = tArchiveUserPty.getValue().getString();
							        Calendar archiveCalender = tArchiveCalenderPty.getValue().getDate();
							        
							        Archive tArchive = new Archive();
							        tArchive.setName(tArchiveName);
							        tArchive.setNodeName(tArchiveNodeName);
							        tArchive.setTitle(tArchiveTitle);
							        tArchive.setUser(tArchiveUser);
							        tArchive.setCalendar(archiveCalender);
							        
							        archiveModelMap.put(tArchiveNodeName,tArchive);
					            }
					        }
					        actionRequest.getPortletSession().setAttribute(ArchiveUploadHelper.ARCHIVE_MODEL_MAP,archiveModelMap);
				        }
					} catch (LoginException e) {
						e.printStackTrace();
					} catch (RepositoryException e) {
						e.printStackTrace();
					} finally {
						if(session!=null)session.logout();
			        }
				}
				return ArchiveUploadHelper.SUCCESS;
			} catch (Exception e) {
				return ArchiveUploadHelper.ERROR;
			}
		}else{
			return ArchiveUploadHelper.ERROR;			
		} 
	}

}
